<?php

declare(strict_types=1);

namespace App\Http\Admin\Controller\User;


use App\Http\Admin\Middleware\AuthMiddleware;
use App\Http\Common\Controller\AbstractController;
use App\Http\Common\Result;
use App\Request\auth\Service\User\UserService;
use App\Request\user\UserRequest;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\PutMapping;
use Hyperf\Validation\Annotation\Scene;

#[Middlewares([
    AuthMiddleware::class
])]
#[Controller(prefix: '/adminapi/user/user/')]
class UserController extends AbstractController
{
    public function __construct(
        protected readonly UserService $service
    )
    {
    }

    /**
     * 用户列表
     * @return Result
     */
    #[GetMapping("lists")]
    public function lists(): Result
    {
        return $this->success(
            $this->service->page($this->request->all(),
                $this->getCurrentPage(),
                $this->getPageSize()
            )
        );
    }

    /**
     * 编辑用户
     * @param UserRequest $request
     * @return Result
     */
    #[Scene(scene: 'update', argument: 'request')]
    #[PutMapping("update")]
    public function update(UserRequest $request): Result
    {
        $this->service->updateById($request->input('id'), $request->all());
        return $this->success([], '更新成功');
    }

    /**
     * 删除用户
     * @param UserRequest $request
     * @return Result
     */
    #[Scene(scene: 'delete')]
    #[DeleteMapping("delete")]
    public function delete(UserRequest $request): Result
    {
        $this->service->deleteById($request->input('id'));
        return $this->success([], '删除成功');
    }
}
